package org.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.dao.AuthorDao;
import org.example.entity.Author;
import org.springframework.stereotype.Service;
import org.example.service.AuthorService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class AuthorServiceImpl implements AuthorService {

    @Resource
    AuthorDao authorDao;

    @Override
    public Boolean addAuthor(Author author) {
        return authorDao.insert(author)>0;
    }

    @Override
    public Boolean deleteAuthor(Long authorId) {
        return authorDao.deleteById(authorId)>0;
    }

    @Override
    public Boolean updateAuthor(Author author) {
        return authorDao.updateById(author)>0;
    }

    @Override
    public Author getAuthorById(Long authorId) {
        return authorDao.selectById(authorId);
    }

    @Override
    public Author getAuthorByName(String name) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("name",name);
        List<Author> authors = authorDao.selectByMap(map);
        if(authors!=null){
            return authors.get(0);
        }
        return null;
    }

    @Override
    public List<Author> fuzzySearch(String field) {
        QueryWrapper<Author> wrapper = new QueryWrapper<>();
        wrapper.like("name",field);
        return authorDao.selectList(wrapper);
    }
}
